package com.ruoyi.common.web.component;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Async
@Component
public class SmsComponent {
    public boolean send(Map<String,String> map, String phone) {
//        if(!StringUtils.isEmpty(phone)) return false;
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(map.get("ACCESS_KEY_ID"))
                // 您的AccessKey Secret
                .setAccessKeySecret(map.get("ACCESS_KEY_SECRET"));
        // 访问的域名（固定值这个不用变都是这个）
        config.endpoint = "dysmsapi.aliyuncs.com";

        Client client;
        try {
                client = new Client(config);
            SendSmsRequest request = new SendSmsRequest();
            request.setSignName(map.get("SIGN_NAME"));          //签名名称
            request.setTemplateCode(map.get("TEMPLATE_CODE"));  //模版
            request.setPhoneNumbers(phone);                     //电话号码
//            request.setTemplateParam("{\"code\":\""+map.get("code")+"\"}");

            Map<String, String> params = new HashMap<>();
            params.put("code",  map.get("code"));
            request.setTemplateParam(JSONObject.toJSONString(params));

//            client.sendSms(request);
//            System.out.println("发送成功："+new Gson().toJson(response));
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
